package com.yingyi.org.service.impl;

import com.yingyi.basic.service.impl.BaseServiceImpl;
import com.yingyi.org.domain.Department;
import com.yingyi.org.mapper.DepartmentMapper;
import com.yingyi.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    
    @Autowired
    private DepartmentMapper departmentMapper;
    
    @Override
    @Transactional
    public void insert(Department dept) {
        /**
         * 对dirpath字段的处理
         * 思路：
         *  1. 顶级部门的ditPath是/id
         *  2. 子级部门的ditPath是/parent'dirPath/id
         *  注意：添加时需要获取自增长的id
         */
        Department temp = new Department();
        //获取自增长的主键id
        departmentMapper.insert(temp);
        dept.setId(temp.getId());

        /*Long parent_id;
        if((parent_id = dept.getParent_id()) != null){
            //前端选择了上级部门

            Department parent = departmentMapper.queryById(parent_id);
            dept.setDirPath(parent.getDirPath().concat("/" + dept.getId()));
        }else{
            //前端没有选择上级部门
            dept.setDirPath("/"+dept.getId());
        }

        //更新dirPath字段
        departmentMapper.update(dept);*/

        //在update方法中也要对dirPath做同样的处理
        update(dept);
    }

    @Override
    @Transactional
    public void update(Department dept) {


        Long parent_id;

        if((parent_id = dept.getParent_id()) != null){
            //前端选择了上级部门

            Department parent = departmentMapper.queryById(parent_id);
            dept.setDirPath(parent.getDirPath().concat("/" + dept.getId()));
        }else{
            //前端没有选择上级部门
            dept.setDirPath("/"+dept.getId());
        }

        //更新dirPath字段
        departmentMapper.update(dept);
    }
    

    /**
     * 获取部门树信息
     *  思路：
     *  1. 一次性查询出所有数据，放到内存中，可以使用map
     *  2. 从所有数据中找出第一层级/顶级部门：这就是我们需要返回的数据
     *  3. 遍历所有部门只要不是第一层级的，找自己的上级，并将自己放入上级的children中
     *  注意：当前部门不能选择自己为上级部门，不应该在上级部门的选择中有自己的本身的选项 【在controller层处理】
     * @return
     */
    @Override
    public List<Department> getDeptTree() {
        //1.1 get all departments
        List<Department> departments = departmentMapper.queryAll();
        //1.2 save all departments to a map with id and department
        Map<Long, Department> map = new HashMap<>();

        for (Department department : departments) {
            map.put(department.getId(), department);

        }


        ArrayList<Department> deptTree = new ArrayList<>();
        //2.1 get top department
        //2.2 get it's children
        for (Department department : departments) {
           if(department.getParent_id() == null) {
               //top department
               deptTree.add(department);

           } else {
               //children department
               //get it's parent department then save it into parent's children field
               Long parent_id = department.getParent_id();
               Department parent = map.get(parent_id);
               parent.getChildren().add(department);
           }
        }

        return deptTree;
    }


}
